package com.cow.sram.comm.config.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.cow.framework.core.base.BaseComponent;
import com.cow.framework.core.cache.RedisUtil;
import com.cow.framework.core.config.dict.DictUtil;
import com.cow.framework.utils.convert.ConvertUtil;
import com.cow.sram.data.dao.TreeDefDAO;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.support.dto.OrgInfoDTO;
 
/**
 * 树形参数初始化
 *
 * @author yangsj
 */
@Component
public class TreeDefSchedule extends BaseComponent{
	@Autowired
	DictUtil dictUtil;
	@Autowired
	protected RedisUtil redisUtil;
	@Autowired
	TreeDefDAO treeDefDAO;	
	
	@Scheduled(initialDelay=1000, fixedRate=600000) 
    public void init(){		
		Map<String,String> treeDataMap = dictUtil.getDict("treeDataType");
		redisUtil.removePattern("treeDef-*");
		for(String key:treeDataMap.keySet()){
			List<Resourceinfo> treeDefList = treeDefDAO.getTreeDefByType(key);
			refreshTree(key,treeDefList);			
		}
		
		orgInit();
    }
	
	/**组织机构数据初始化*/
    public void orgInit(){
		List<OrgInfoDTO> orgList = super.getAuthClient().getOrgs();
		List<Resourceinfo> treeDefList = new ArrayList<Resourceinfo>();		
		//转为tree
		for(OrgInfoDTO org:orgList){
			Resourceinfo tree = new Resourceinfo();
			tree.setResourcetype("t10");
			tree.setResourceid(org.getOrgid().intValue());
			tree.setName(org.getOrgname());
			tree.setFatherId(org.getSuperorgid().intValue());
			treeDefList.add(tree);
		}		
		refreshTree("t10",treeDefList);	
    }
	
    /**设置子节点标志*/	
	private void setHasChild(List<Resourceinfo> treeList){		
		Map<Integer,Resourceinfo> treeMap = new HashMap<Integer,Resourceinfo>();
		for(Resourceinfo tree:treeList){
			treeMap.put(tree.getFatherId(), tree);
		}
		
		for(Resourceinfo tree:treeList){
			if(treeMap.get(tree.getResourceid())!=null){
				tree.setHasChild(1);
			}else{
				tree.setHasChild(0);
			}
		}
	}
	/**设置缓存*/
    public void refreshTree(String key,List<Resourceinfo> treeDefList){		
		String fatherId="init";
		setHasChild(treeDefList);
		List<Resourceinfo> treeList = null;
		//数据初始化
		for(Resourceinfo tree:treeDefList){
			if(!fatherId.equals(ConvertUtil.nvl(tree.getFatherId(),"").toString())){
				if(!fatherId.equals("init")){
					redisUtil.set("treeDef-"+tree.getResourcetype()+"-"+fatherId,treeList); 
				}
				
				fatherId = ConvertUtil.nvl(tree.getFatherId(),"").toString();
				treeList = (List<Resourceinfo>)redisUtil.get("treeDef-"+tree.getResourcetype()+"-"+fatherId); 
				if(treeList == null){
					treeList = new ArrayList<Resourceinfo>();					
				}
			}
							
			treeList.add(tree);
			
		}
		redisUtil.set("treeDef-"+key+"-"+fatherId,treeList); 
    }
	/**设置缓存*/
    public void refreshCommTree(String key,List<Resourceinfo> treeDefList){		
		String fatherId="init";
		setHasChild(treeDefList);
		List<Resourceinfo> treeList = null;
		//数据初始化
		for(Resourceinfo tree:treeDefList){
			if(!fatherId.equals(ConvertUtil.nvl(tree.getFatherId(),"").toString())){
				if(!fatherId.equals("init")){
					redisUtil.setCommon("treeDef-"+tree.getResourcetype()+"-"+fatherId,treeList); 
				}
				
				fatherId = ConvertUtil.nvl(tree.getFatherId(),"").toString();
				treeList = (List<Resourceinfo>)redisUtil.getCommon("treeDef-"+tree.getResourcetype()+"-"+fatherId); 
				if(treeList == null){
					treeList = new ArrayList<Resourceinfo>();					
				}
			}
							
			treeList.add(tree);
			
		}
		redisUtil.setCommon("treeDef-"+key+"-"+fatherId,treeList); 
    }
	
}
